from django.db import models
import datetime


class BaseModel(models.Model):
    """
    基础模型，包含创建时间和更新时间字段
    """
    is_delete = models.BooleanField('数据逻辑状态', default=True)
    create_time = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="创建时间")
    updated_time = models.DateTimeField(auto_now_add=False, auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True  # 表示这是一个抽象类，不会生成对应的表
        ordering = ['-create_time']


class Account(BaseModel):
    name = models.CharField('姓名', max_length=20, blank=True, null=True, default="")
    pwd = models.CharField("密码", max_length=50, blank=True, null=True, default="")
    mobile = models.CharField('手机号', max_length=100, unique=True, blank=True, null=True, default="")
    token = models.CharField("Token", max_length=50, blank=True, null=True, default="")
    role_choice = ((1, "学生"), (2, "老师"), (3, "老师"))
    role = models.SmallIntegerField("角色", choices=role_choice, default=1)

    class Meta:
        db_table = 'aas_account'
        verbose_name = '账户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Log(BaseModel):
    account = models.ForeignKey(Account, on_delete=models.DO_NOTHING, db_constraint=False)
    last_login_time = models.DateTimeField("最后登录时间", auto_now_add=False, auto_now=True)

    class Meta:
        db_table = 'aas_log'
        verbose_name = '账户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class School(BaseModel):
    """
    学校基础模型
    """
    name = models.CharField('学校名称', max_length=100, blank=True, null=True, default="")
    is_active = models.BooleanField('激活状态', default=True)
    contact_name = models.CharField('负责人姓名', max_length=100, blank=True, null=True, default="")
    contact_tel = models.CharField('负责人电话', max_length=100, blank=True, null=True, default="")
    address = models.CharField('学校地址', max_length=200, blank=True, null=True, default="")
    avatar = models.ImageField("学校图片", upload_to="avatar/school/%Y", blank=True, null=True, default="")
    description = models.TextField('学校简介', blank=True, null=True, default="")

    class Meta:
        db_table = 'aas_school'
        verbose_name = '学院表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Teacher(BaseModel):
    """
    教师基础模型
    """
    name = models.CharField('姓名', max_length=20, blank=True, null=True, default="")
    pwd = models.CharField("密码", max_length=50, blank=True, null=True, default="")
    mobile = models.CharField('手机号', max_length=100, unique=True, blank=True, null=True, default="")
    # token = models.CharField("Token", max_length=50, blank=True, null=True, default="")
    status_staff = models.BooleanField('在职状态', default=True)
    teacher_role = models.IntegerField("教师类型", choices=((1, "助教老师"), (2, "授课老师")), default=1)

    class Meta:
        db_table = 'aas_teacher'
        verbose_name = '教师表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class TeacherDetail(BaseModel):
    """
    教师详情基础模型
    """
    name = models.CharField('姓名', max_length=100, blank=True, null=True, default="")
    nick_name = models.CharField('别名', max_length=100, blank=True, null=True, default="")
    pwd = models.CharField("密码", max_length=100, blank=True, null=True, default="")
    age = models.IntegerField("年龄", blank=True, null=True, default=20)
    gender = models.SmallIntegerField("性别", choices=((1, "男"), (2, "女")), default=1)
    avatar = models.ImageField("个人头像", upload_to="avatar/teacher/%Y", blank=True, null=True, default="")
    qq = models.CharField('QQ号', max_length=100, blank=True, null=True, default="")
    dingding = models.CharField('钉钉号', max_length=100, blank=True, null=True, default="")
    feishu = models.CharField('飞书号', max_length=100, blank=True, null=True, default="")
    wechat_name = models.CharField("微信名称", max_length=100, blank=True, null=True, default="")
    wechat_id = models.CharField("微信ID", max_length=100, blank=True, null=True, default="")
    email = models.CharField('邮箱', max_length=100, blank=True, null=True, default="")
    graduation_school = models.CharField("毕业学校", max_length=15, blank=True, null=True, default="")
    edu_choices = (("专科", "专科"), ("本科", "本科"), ("硕士", "硕士"), ("博士", "博士"))
    education = models.CharField("学历", max_length=100, choices=edu_choices, default="学士")
    major = models.CharField('专业', max_length=100, blank=True, null=True, default="")
    certifications = models.CharField('证书', max_length=100, blank=True, null=True, default="")
    skill = models.CharField('技能', max_length=100, blank=True, null=True, default="")
    marital_status = models.IntegerField('婚姻', choices=((1, "未婚"), (2, "已婚")), default=1)
    ecp = models.CharField('紧急联系人姓名', max_length=100, blank=True, null=True, default="")
    ucp = models.CharField('紧急联系人电话', max_length=100, blank=True, null=True, default="")
    birth_place = models.CharField('籍贯', max_length=100, blank=True, null=True, default="")
    nationality = models.CharField("民族", max_length=100, blank=True, null=True, default="")
    home_address = models.CharField('家庭住址', max_length=100, blank=True, null=True, default="")
    identity_card_address = models.CharField('身份证地址', max_length=100, blank=True, null=True, default="")
    birthday = models.DateField("出生日期", max_length=100, blank=True, null=True, default="")
    date_hire = models.DateField('入职日期', max_length=100, blank=True, null=True, default=datetime.date.today)
    date_fire = models.DateField('离职日期', max_length=100, null=True, blank=True, default=datetime.date.today)
    description = models.TextField('备注描述', max_length=100, null=True, blank=True, default="")
    teacher = models.ForeignKey(Teacher, on_delete=models.DO_NOTHING, db_constraint=False)
    school = models.ForeignKey(School, on_delete=models.DO_NOTHING, db_constraint=False)

    class Meta:
        db_table = 'aas_teacher_detail'
        verbose_name = '教师详情表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Student(BaseModel):
    """
    教师基础模型
    """
    name = models.CharField('姓名', max_length=20, blank=True, null=True, default="")
    pwd = models.CharField("密码", max_length=50, blank=True, null=True, default="")
    mobile = models.CharField('手机号', max_length=100, unique=True, blank=True, null=True, default="")
    token = models.CharField("Token", max_length=50, blank=True, null=True, default="")
    status_staff = models.BooleanField('入学状态', default=True)

    # last_login_time = models.DateTimeField("最后登录时间", auto_now_add=False, auto_now=True)

    class Meta:
        db_table = 'aas_student'
        verbose_name = '学生表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class StudentDetail(BaseModel):
    """
    学生基础模型
    """
    name = models.CharField('姓名', max_length=100, blank=True, null=True, default="")
    nick_name = models.CharField('别名', max_length=100, blank=True, null=True, default="")
    age = models.IntegerField("年龄", blank=True, null=True, default=20)
    gender = models.SmallIntegerField("性别", choices=((1, "男"), (2, "女")), default=1)
    avatar = models.ImageField("个人头像", upload_to="avatar/student/%Y", blank=True, null=True, default="")
    qq = models.CharField('QQ号', max_length=100, unique=True, blank=True, null=True, default="")
    dingding = models.CharField('钉钉号', max_length=100, blank=True, null=True, default="")
    feishu = models.CharField('飞书号', max_length=100, blank=True, null=True, default="")
    wechat_name = models.CharField("微信名称", max_length=100, blank=True, null=True, default="")
    wechat_id = models.CharField("微信ID", max_length=100, blank=True, null=True, default="")
    email = models.CharField('邮箱', max_length=100, blank=True, null=True, default="")
    graduation_school = models.CharField("学校", max_length=50, blank=True, null=True, default="")
    edu_choices = ((1, "小学"), (2, "初中"), (3, "高中"), (4, "专科"), (5, "本科"), (5, "硕士"), (7, "博士"))
    education = models.IntegerField("学历", choices=edu_choices, default="学士")
    major = models.CharField('专业', max_length=100, blank=True, null=True, default="")
    certifications = models.CharField('证书', max_length=100, blank=True, null=True, default="")
    skill = models.CharField('技能', max_length=100, blank=True, null=True, default="")
    ecp = models.CharField('紧急联系人姓名', max_length=100, blank=True, null=True, default="")
    ucp = models.CharField('紧急联系人电话', max_length=100, blank=True, null=True, default="")
    birth_place = models.CharField('籍贯', max_length=100, blank=True, null=True, default="")
    nationality = models.CharField("民族", max_length=100, blank=True, null=True, default="")
    home_address = models.CharField('家庭住址', max_length=100, blank=True, null=True, default="")
    identity_card_address = models.CharField('身份证地址', max_length=100, blank=True, null=True, default="")
    description = models.TextField('备注描述', max_length=500, null=True, blank=True, default="")
    birthday = models.DateField("出生日期", blank=True, null=True, default="")
    student = models.ForeignKey(Student, on_delete=models.DO_NOTHING, db_constraint=False)
    teacher = models.ManyToManyField(Teacher, db_constraint=False)
    school = models.ForeignKey(School, on_delete=models.DO_NOTHING, db_constraint=False, default="")
    last_login_time = models.DateTimeField("最后登录时间", auto_now_add=False, auto_now=True)

    class Meta:
        db_table = 'aas_student_detail'
        verbose_name = '学生详情表'
        verbose_name_plural = verbose_name

    def teacher_name(self):
        """
        获取老师名称
        """
        self.verbose_name = '老师名称'
        return self.teacher.name

    def __str__(self):
        return self.name


class StudentRelation(BaseModel):
    """
    学生亲属模型
    """
    name = models.CharField('姓名', max_length=100, blank=True, null=True, default="")
    nick_name = models.CharField('别名', max_length=100, blank=True, null=True, default="")
    age = models.IntegerField("年龄", blank=True, null=True, default=20)
    gender = models.IntegerField("性别", choices=((1, "男"), (2, "女")), default=1)
    mobile = models.CharField('手机号', max_length=100, unique=True, blank=True, null=True, default="")
    avatar = models.ImageField("个人头像", upload_to="avatar/student/%Y", blank=True, null=True, default="")
    qq = models.CharField('QQ号', max_length=100, unique=True, blank=True, null=True, default="")
    dingding = models.CharField('钉钉号', max_length=100, blank=True, null=True, default="")
    feishu = models.CharField('飞书号', max_length=100, blank=True, null=True, default="")
    wechat_name = models.CharField("微信名称", max_length=100, blank=True, null=True, default="")
    wechat_id = models.CharField("微信ID", max_length=100, blank=True, null=True, default="")
    email = models.CharField('邮箱', max_length=50, blank=True, null=True, default="")
    graduation_school = models.CharField("毕业学校", max_length=50, blank=True, null=True, default="")
    edu_choices = ((1, "小学"), (2, "初中"), (3, "高中"), (4, "专科"), (5, "本科"), (5, "硕士"), (7, "博士"))
    education = models.IntegerField("学历", choices=edu_choices, default=5)
    major = models.CharField('专业', max_length=100, blank=True, null=True, default="")
    certifications = models.CharField('证书', max_length=100, blank=True, null=True, default="")
    skill = models.CharField('技能', max_length=100, blank=True, null=True, default="")
    birth_place = models.CharField('籍贯', max_length=100, blank=True, null=True, default="")
    nationality = models.CharField("民族", max_length=100, blank=True, null=True, default="")
    home_address = models.CharField('家庭住址', max_length=100, blank=True, null=True, default="")
    identity_card_address = models.CharField('身份证地址', max_length=100, blank=True, null=True, default="")
    description = models.TextField('备注描述', max_length=100, null=True, blank=True, default="")
    student = models.ForeignKey(Student, on_delete=models.DO_NOTHING, db_constraint=False)

    class Meta:
        db_table = 'aas_student_relation'
        verbose_name = '学生亲属详情表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Course(BaseModel):
    name = models.CharField('课程名称', max_length=100, null=True, blank=True, default="")
    is_active = models.BooleanField('激活状态', default=True)
    # teacher = models.ForeignKey(Teacher, on_delete=models.DO_NOTHING, db_constraint=False, default="")
    # school = models.ForeignKey(School, on_delete=models.DO_NOTHING, db_constraint=False, default="")
    avatar = models.ImageField("课程图片", upload_to="avatar/course/%Y", blank=True, null=True, default="")
    description = models.TextField('课程描述', null=True, blank=True, default="")
    course_section = models.IntegerField('课程数', blank=True, null=True, default=0)
    course_duration = models.IntegerField('课程时长', blank=True, null=True, default=0)
    course_learners = models.IntegerField('学习人数', blank=True, null=True, default=0)
    course_price = models.IntegerField('课程价格', blank=True, null=True, default=0)
    course_url = models.CharField('课件地址', max_length=100, blank=True, null=True, default=0)

    class Meta:
        db_table = 'aas_courses'
        verbose_name = '课程表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseDetail(BaseModel):
    name = models.CharField('课程表', max_length=100, null=True, blank=True, default="")
    is_active = models.BooleanField('激活状态', default=True)
    teacher = models.ManyToManyField(Teacher, db_constraint=False, default="")
    student = models.ManyToManyField(Student, db_constraint=False, default="")
    school = models.ForeignKey(School, on_delete=models.DO_NOTHING, db_constraint=False, default="")
    description = models.TextField('课程描述', null=True, blank=True, default="")

    class Meta:
        db_table = 'aas_course_detail'
        verbose_name = '课程表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
